package ru.paswd.marketprice.ui.presenter

import moxy.InjectViewState
import ru.paswd.marketprice.Application
import ru.paswd.marketprice.data.mappers.ProductCategoryMapper
import ru.paswd.marketprice.data.repositories.ProductCategoriesRepository
import ru.paswd.marketprice.ui.view.ProductsListView
import java.util.stream.Collectors

@InjectViewState
class ProductsListPresenter : BasePresenter<ProductsListView>() {
    fun onListRefresh() {
        ProductCategoriesRepository.requestAll(Application.instance!!.applicationContext!!
        ) { success, result ->
            if (success) {
                viewState?.updateCategories(
                        result
                                ?.stream()
                                ?.map { ProductCategoryMapper.fromDto(it) }
                                ?.collect(Collectors.toList())
                                ?: listOf())
            }

            viewState?.onRefreshed()
        }
    }

    fun onReceiptScanPressed() {
        viewState?.showReceiptScanView()
    }
}